<?php
namespace Common\Model;

/**
 * 客栈扩展信息表
 * @modify trainyao 2016-04-29 创建
 * @index  hotelId(`hotelentity_id`);
 */

Class HotelInfoModel extends BaseModel
{
    protected $trueTableName = 'tb_hotel_info';
    
    /**
     * 新建信息
     * @modify trainyao 2016-04-29 创建
     * @param  integer   $hotelId   客栈ID
     * @return mixin        执行成功返回id
     */
    public function addInfo($hotelId) {
        $data = array(
            'hotelentity_id' => $hotelId,
        );
        $id = $this->add($data);
        return $id === false ? false : $id;
    }
    

    /**
     * 根据客栈获取信息
     * @modify trainyao 2016-04-29 创建
     * @param  integer   $hotelId   客栈ID
     * @return array              信息数组
     */
    public function getInfoByHotel($hotelId) {
        if(is_array($hotelId)) {
            $hotelId = implode(',', $hotelId);
        }
        if (strpos($hotelId, ',') !== false) {
            $option['hotelentity_id'] = array('in', $hotelId);
            $data = $this->where($option)->select();
        } else {
            $option['hotelentity_id'] = $hotelId;
            $data = $this->where($option)->find();
        }
        
        return empty($data) ? array() : $data;
    }

    /**
     * 根据客栈保存数据
     * @modify wenhui 2017-08-28 创建
     * @param  integer   $hotelId   客栈ID
     * @param  string   $checkoutTime      客栈最迟退房时间
     * @param  string   $checkinTime      客栈最早入住时间
     * @return bool
     */
    public function saveInfo($hotelId, $checkoutTime = null, $checkinTime = null) {
        $option = array(
            'hotelentity_id' => $hotelId,
        );

        $data = array();
        if (!is_null($checkoutTime)) {
            $data['checkouttime'] = $checkoutTime;
        }
        if (!is_null($checkoutTime)) {
            $data['checkintime'] = $checkinTime;
        }

        $result = $this->where($option)->save($data);
        
        return $result === false ? false : true;
    }
}
